﻿using System;
using System.Linq;
using System.Windows.Forms;
using Coffee_Restaurant.Database;

namespace Coffee_Restaurant.Forms.AreaTable
{
    public partial class TableAddEdit : Form
    {
        //event edit
        public delegate void ButtonSaveEventHandler(string name);
        public event ButtonSaveEventHandler BtnAddTable;

        //event save
        public delegate void ButtonSaveEditEventHandler(string oldname, string areaId, string name);
        public event ButtonSaveEditEventHandler BtnEditTable;

        //idEdit
        public string numberTable { get; set; }

        public int? AreaId { get; set; }

        public TableAddEdit()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        public bool ValidateForm()
        {
            if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrWhiteSpace(txtName.Text))
            {
                txtName.BackColor = System.Drawing.Color.Khaki;
                txtName.Focus();
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (btnSave.Name == "btnSaveEdit")
            {
                if (BtnEditTable != null && ValidateForm() && !string.IsNullOrEmpty(numberTable))
                {
                    try
                    {
                        using (var db = new DatabaseEntities())
                        {
                            //save to database
                            var model = db.Tables.FirstOrDefault(x => x.Name == numberTable);
                            if (model != null)
                            {
                                model.AreaId = int.Parse(cbbArea.SelectedValue.ToString());
                                model.Name = txtName.Text;
                                model.Description = txtDescription.Text;
                            }
                            if (db.SaveChanges() > 0)
                            {
                                string oldname = numberTable;
                                if (model != null) BtnEditTable(oldname, model.AreaId.ToString(), model.Name);
                                this.Hide();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        BtnEditTable(null, null, null);
                    }
                }
            }
            else
            {
                if (BtnAddTable != null && ValidateForm())
                {
                    try
                    {
                        using (var db = new DatabaseEntities())
                        {

                            //add to database
                            var guid = Guid.NewGuid().ToString();
                            db.Tables.Add(new Table() { AreaId = int.Parse(cbbArea.SelectedValue.ToString()), Name = txtName.Text, Icon = "", Description = txtDescription.Text, Date = DateTime.Now, GuiId = guid });
                            if (db.SaveChanges() > 0)
                            {
                                var model = db.Tables.FirstOrDefault(a => a.GuiId == guid);
                                if (model != null)
                                {
                                    BtnAddTable(model.Name);
                                }
                                this.Hide();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        BtnAddTable(null);
                    }
                }
            }
        }


        //esc to exit
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void TableAddEdit_Load(object sender, EventArgs e)
        {
            using (var db = new DatabaseEntities())
            {
                var model = db.Areas.ToList();
                cbbArea.DataSource = model;
                cbbArea.DisplayMember = "Name"; //ten truong hien thi
                cbbArea.ValueMember = "AreaId"; //ten cot gia tri
                cbbArea.SelectedValue = AreaId; //default select when click treeview

                //neu la hanh dong edit thi khong co gia tri default so ban lon nhat
                if (btnSave.Name == "btnEdit")
                {
                    //tim kiem so ban lon nhat
                    var modelTable = db.Tables.OrderByDescending(t => t.Date).Take(1).FirstOrDefault();
                    if (modelTable != null)
                    {
                        int number = int.Parse(modelTable.Name);
                        number = number + 1;
                        txtName.Text = number.ToString();
                    }
                }
            }
        }
    }
}
